#Merging and cleaning experiment 1 and experiment 2 data
#Alan Yan
#3-16-2020

#clear environment
rm(list = ls())

#load libraries
library(pacman)
p_load(tidyverse)

#load data
exp1 <- read.csv("01-Experiment-1/data/04-clean-data/clean_data.csv", header = TRUE, stringsAsFactors = FALSE)
exp2 <- read.csv("02-Experiment-2/data/04-clean-data/clean_data.csv", header = TRUE, stringsAsFactors = FALSE)

#renaming "texter_id" in study 2 to match "texter.id"
names(exp2)[9] <- "texter.id"

#change texter IDs from study 2 to distinguish between study 1 and study 2 when we merge
max.texter.id <- unique(exp1$texter.id) %>% max()
exp2$texter.id <- exp2$texter.id + max.texter.id

#create a dummy variable to represent fixed effects
exp1$experiment1 <- 1
exp2$experiment1 <- 0

#create binary offensive variable
exp1$offensive <- exp1$Offensive
exp1$offensive.orig <- exp1$Offensive
exp2$offensive.orig <- exp2$offensive.index
exp2$offensive <- ifelse(exp2$offensive.index>0, 100, 0)

#creat discouraging column
exp1$discouraging <- 0
exp2$discouraging <- exp2$discouraging.index

#keep variables
keep.vars <- c("silenced", "offensive", "male", "female", "gen.neutral", "no.name", "male.instrument",
               "female.instrument", "gen.neutral.instrument", "no.name.instrument", "gender", 
               "texter.id", "texter.gender", "experiment1", "responded", "offensive.orig", "discouraging")

#merging the two datasets
dt <- rbind(exp1[keep.vars],
            exp2[keep.vars])

dt$gender <- ifelse(is.na(dt$gender) == TRUE, "Unknown", dt$gender)

#create withdrawal variable (silencing = 100 and offensiveness = 0)
dt$withdrawal <- ifelse(dt$silenced == 100 & dt$offensive == 0, 100, 0)

#create a pure silencing (?) variable (silencing = 100 and offensiveness = 100)
dt$pure.silencing <- ifelse(dt$silenced == 100 & dt$offensive == 100, 100, 0)

write.csv(dt, "03-Silencing-Study/data/01-clean-data/clean_data.csv")



